package com.bolt.support.sqlconfig.dialect;


import com.bolt.common.db.DbHelper;
import com.bolt.convention.exception.ServiceException;

/**
 * Created by Administrator on 2017/8/16.
 */
public final class PagingDialectFactory {

    public static PagingDialect getDialect(String dbUrl) {
        PagingDialect.Type type;
        if (DbHelper.dbTypeIsMySQL(dbUrl)) {
            type = PagingDialect.Type.MYSQL;
        } else if (DbHelper.dbTypeIsOracle(dbUrl)) {
            type = PagingDialect.Type.ORACLE;
        } else if (DbHelper.dbTypeIsPostgre(dbUrl)) {
            type = PagingDialect.Type.POSTGRESQL;
        } else {
            throw new ServiceException("不支持的数据库类型");
        }
        return buildDialect(type);
    }

    public static PagingDialect buildDialect(PagingDialect.Type dialectType) {
        switch (dialectType) {
            case MYSQL:
                return new PagingMySQLDialect();
            case ORACLE:
                return new PagingOracleDialect();
            case POSTGRESQL:
                return new PagingPostgreSqlDialect();
            default:
                throw new UnsupportedOperationException();
        }
    }
}
